package com.mc.music_web.controller;

import com.github.pagehelper.PageInfo;
import com.mc.music_web.entity.User;
import com.mc.music_web.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/userLogin")
    public String login(){

        return "/user/userLogin";
    }
    @RequestMapping("/userMV")
    public String loginMV(){

        return "/user/mv";
    }

    @RequestMapping("/index")
    public String index(){

        return "user/index";
    }
    @RequestMapping("/userSingerLogin")
    public String userSingerLogin(){

        return "/user/singer";
    }
    @RequestMapping("/songTypeLogin")
    public String songTypeLogin(){

        return "/user/songtype";
    }
    @RequestMapping("/ticketLogin")
    public String ticketLogin(){

        return "/user/ticket";
    }
    @RequestMapping("/doUserLogin")
    @ResponseBody
    public String doLogin(User user, HttpSession session){
        //获取service判断登录的结果

        User user1=userService.UserLogin(user);

        if (user1.getUsersName()!=null&&user1.getPassword()!=null) {
            //将用户对象添加到Session中
            session.setAttribute("USER_SESSION", user1);
            return "/index";
        }
        return "/doUserLogin";

    }

     //注册
     @RequestMapping("/register")
     public String Register(){
         return "/user/register";
     }
      @RequestMapping(value = "/user/toRegister")
      @ResponseBody
     public Integer toRegister(@ModelAttribute User user, HttpServletRequest request){
          String realpath = request.getServletContext().getRealPath("fileUpload");
          String fileName = user.getMyfile().getOriginalFilename();
          System.out.println(fileName);
          File targetFile = new File(realpath, fileName);
          System.out.println(targetFile);
          if(!targetFile.exists()){
              targetFile.mkdirs();
              System.out.println(targetFile+"创建成功");
          }
          String picUrl = "http://localhost:8080/mc-web_war/fileUpload/"+fileName;
          System.out.println("访问图片的路径"+picUrl);
          try {
              user.getMyfile().transferTo(targetFile);
          } catch (Exception e) {
              e.printStackTrace();
          }

        return userService.regeriter(user);
      }

    @PostMapping("/user/register")
    public String add(@Valid @ModelAttribute User user, BindingResult result){
        //判断数据验证的结果result对象
        if (result.hasErrors()){
            return "/user/register";
        }

        return "user/index";
    }

    @RequestMapping("/admin/lastUser")
    public  String goUser(){

        return "/admin/user-list";
    }

    @RequestMapping("/admin/userList")
    @ResponseBody
    public List userList(){
        List<User> list=userService.sellectUser();
        return list;
    }

    @RequestMapping("/admin/goUserUpdate")
    @ResponseBody
    public User updateUser(Integer userId) {

        User user = userService.selectByPrimaryKeyUser(userId);

        return user;
    }


    @RequestMapping("/admin/doUserUpdate")
    @ResponseBody
    public User doUpdateUser(User user) {

        userService.updateUser(user);
        return user;
    }


    @RequestMapping("/admin/goUserLimit")
    @ResponseBody
    public PageInfo selectUserLimit(int pageNum, HttpSession session) {
        PageInfo<User> pageInfoso = userService.selectUserLimit(pageNum);
        session.setAttribute("songSession",pageInfoso);
        return pageInfoso;
    }


    @RequestMapping("/admin/userDel")
    @ResponseBody
    public int delUser(Integer userId) {
        int result = userService.delterUser(userId);

        return result;
    }
    @RequestMapping("/admin/adminByUser")
    @ResponseBody
    public  List  selectBySinger(String keyword){
        List<User> list=userService.selectByChooseUser(keyword);
        return list;
    }
}
